/*
 Copyright (c) 2009, Ivan Popivanov
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.

 * Neither the name of StockCandles nor the names of its contributors
 may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef APPENGN_H
#define APPENGN_H

#ifdef _MSC_VER
#pragma warning(disable:4996)
#endif // _MSC_VER

#include <QTextStream>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QObject>
#include <QFile>

#include "Ohlc.h"
#include "MainWindow.h"
#include "ChartView.h"

class AppEngn : public QObject
{
   Q_OBJECT

public:
   static AppEngn *instance()
   {
      if(appEngn == NULL)
      {
         appEngn = new AppEngn();
      }

      return appEngn;
   }

public:
   void initGUI();
   void startGUI();

   void log(QtMsgType msgType, const char *msg);

public slots:
   QString loadFromYahooCsv(const QString &symbol);
   QString computeReturn(const QString &endDate, const QString &startDate, double factor, double error, bool showDetails = false);
   QString listTAFunctions();
   QString loadSymbol(const QString &symbol, bool update = false);
   QString updateStocks(const QString &dir);
   QString updateStockList(const QStringList &stocks, const QString &outDir);
   QString computeStochastics(const QString &symbol, const QString &outFile, int k = 14, int d = 3, int smooth = 3);
   QString computeMA(const QString &inPath, const QString &outDir);
   QString createDb(const QString &path);
   QString filter();
   QString computeIndicators(const QString &symbol, const QString &outPath);
   QString dowTheorySignals(const QString &path, const QString &outPath = QString());
   QString refreshIndices(const QString &path = QString(), bool defaultList = false);

   // Benchmarking
   QString generateData(const QString &outPath);
   QString benchmarkCsvReads(const QString &path, int filesToProcess = 100, int linesToProcess = INT_MAX);
   QString benchmarkCsvAppends(const QString &path, int appends);
   QString benchmarkFileIO(const QString &path, int filesToProcess = 100);
   QString benchmarkDb(const QString &path, int symbolsToProcess = 100, int linesToProcess = INT_MAX);
   QString benchmarkFastDbReads(const QString &path, int symbolsToProcess, int linesToProcess);
   QString benchmarkFastDbUpdates(const QString &path, int updates);
   QString loadFastDb(const QString &path, uint numCandles = UINT_MAX);

   void onAddEODData();
   void onClose();

private:
   AppEngn();

   bool download(const QString &symbol, const QDate &startDate, const QString &path);
   bool download(const QString &symbol, const QString &path) { return download(symbol, QDate(1,1,1900), path); }

   // Download from scratch
   bool downloadNew(const QString &symbol, const QString &path);

   bool updateFromYahoo(Ohlc &s, const QString &path);

   bool onlyHolidaysSince(const QDate &startDate);
   QStringList stockListFromDir(const QString &path);

   bool addEODDataFileToDb(const QString &path);

private:
   static AppEngn *appEngn;

private:
   MainWindow *mainWnd;
   ChartView *chartView;

   QFile logFile;

   Ohlc ohlc;

   QSqlDatabase db;
};

#endif // APPENGN_H